package com.copa.l1779;

/**
 * @author copa
 * @createDate 2022-08-29 11:54
 * @function 一次遍历法
 */
public class Test1 {

    public int nearestValidPoint(int x, int y, int[][] points) {
        int min = Integer.MAX_VALUE;
        int result = -1;
        int temp;
        for (int i = 0; i < points.length; i++) {
            if (points[i][0] == x || points[i][1] == y) {
                temp = Math.abs(points[i][0] - x) + Math.abs(points[i][1] - y);
                //result = temp < result ? temp : result;
                if (temp < min) {
                    min = temp;
                    result = i;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int x = 3;
        int y = 4;
        int[][] points = {{1,2},{3,1},{2,4},{2,3},{4,4}};
        System.out.println(new Test1().nearestValidPoint(x, y, points));
    }
}
